comparator and comparable in java interview questions
comparator and comparable in java interview questions
Learn about comparator and comparable in java interview questions, we have the largest and most updated comparator and comparable in java interview questions information on alibabacloud.com
Here is a brief introduction to Java's comparable internal comparator and comparator external comparator usage implementationSo let's do a sort of between objects, first build a model or call Javaben. As follows:The use of the 1.Java com
defined as follows:
Package java.util;
Public interface comparator
Description1. If a class implements the comparator interface: it must implement the CompareTo (t O1, T O2) function, but it may not implement the Equals (Object obj) function.Why not implement the Equals (Object obj) function? Because any class, the default is already implemented equals (Object obj). All classes in
[Common Java class libraries] _ notes on comparator (comparable and comparator)
Objectives of this chapter:Understanding the use of comparable comparison InterfacesUnderstand the basic sorting principle of ComparatorUnderstanding the use of
binarytree{Class node{//Declaration of a Node classprivate comparable data; Save the specific contentPrivate Node left; Save ZuoziPrivate Node right; Save Right subtreePublic Node (comparable data) {This.data = data;}public void AddNode (Node newNode) {Make sure it's on the Zuozi or right subtree.if (NewNode.data.compareTo (this.data) if (this.left==null) {This.left = NewNode; Set the new node directly to
Sometimes you need to compare Collection objects or arrays that are not a single number. There are two methods: 1 is to implement the Comparable interface and 2 is to implement the Comparator interface. 1. the ComParable interface Comparable interface is defined as follows: public interface
".
Comparator Introduction
Comparator is the comparator interface.
If we need to control the order of a class, and the class itself does not support sorting (that is, not implementing the comparable interface), then we can create a "comparer for that class" to sort. This "comparer" only needs to implement the
IntroductionThe Comparator is the comparator interface.If we need to control the order of a class, and the class itself does not support ordering (that is, it does not implement the comparable interface), then we can create a "comparer for this class" to sort by. This "comparator" only needs to implement the
Comparable and comparator interface difference analysis in JavaSource: Code Farming Network | Time: 2015-03-16-10:25:20 | Hits:8902
[Introduction] This article is to be detailed analysis of Java comparable and comparator interface differences, both have a comparativ
classes in Java inherit from Java.lang.Object and implement the Equals (Object obj) function in Object.java, so all other classes are equivalent to implementing the function.() int compare (t O1, T O2) is "Comparing the size of O1 and O2". Returns "negative number", meaning "O1 is smaller than O2"; returning "0" means "O1 equals O2"; returning "positive number" means "O1 is greater than O2".
comparison of C
;import java.util.collection;import java.util.collections;import java.util.Comparator; Import Java.util.hashset;import java.util.list;import java.util.sortedset;import java.util.TreeSet; public class listsortwithcomparator{publicstatic void Main (string[] args) {/* * Use comparator to sort the first Method, * Define a class that implements the comparator interface, and overload the Compara method, set the c
comparable and comparator in Java
-comparable interfaceInterface definitionpublicinterface ComparableT> { publicint compareTo(T o); } The interface contains only one CompareTo () function,int CompareTo (object o): Compares the current instance object with the object o, returns a negative value if it prec
The details and differences of Java comparable and Comparator
Java provides us with two comparison mechanisms: comparable and Comparator, what is the difference between them? Come to understand today.
object class has implemented the Equals methodThe comparable interface provides only the int compareTo (T O) method, which means that if I define a person class that implements the comparable interface, then when I instantiate the person1 of the person class, I want to compare person1 and The size of an existing person object Person2, I can call it this way: Person1.comparto (Person2), can be judged by the
(Object obj) function? Because any class, the default is already implemented equals (Object obj). All classes in Java inherit from Java.lang.Object and implement the Equals (Object obj) function in Object.java, so all other classes are equivalent to implementing the function.
The int compare (t O1, T O2) is the size of the comparison O1 and O2. Returning "negative numbers" means "O1 is smaller than O2"; returning "0" means "O1 equals O2";
after being added to the list container, or the comparable interface may be implemented, and if comparator is not specified in the sort method of the collections class, it is sorted in a natural order. The so-called natural order is the implementation of the comparable interface settings of the ordering mode.Comparator is a dedicated
comparison method for the item object@Override Public int CompareTo(Item Other) {returnInteger.compare (PartNumber, Other.partnumber); }}(2) comprehensive comparable and comparator//Implement comparable interface Public classPerson implements comparablePrivate intNumPrivateString name;Private intAge Public Person(intNum, String name,intAge) {super (); This. num
Tag: null override cannot serial failed RBO entity i++ dataAfter reading this article you will learn:
Comparable natural sort
Comparator Custom Sorting
Summarize
There are two comparison mechanisms available in Java: Comparable and Compa
java.util.arrays;/** * uses comparable interface: Enables the class where the object to be sorted implements the comparable interface, And overriding the CompareTo () method in the comparable interface * The disadvantage is that you can only sort by one rule * @author Tong * */public class Objectsort {public static void main (string[ ] args) {person[] persons =
("u1003")}; Arrays.sort (Users, New Usercomparator ()); for (int i = 0; i Select comparable interface or comparator? A class that implements the comparable interface indicates that objects of this class can be compared to each other, and that a collection of such objects can be sorted directly using the Sort method.Comparator can be regarded
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.